package com.jsp.dao;



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.jsp.bean.UserBean;
import com.jsp.util.DBUtil;






public class FindUserDao {
	public static void main(String[] args){
		List<UserBean> listuserbean=new FindUserDao().find(1);
	}
//	public Connection getConnection() {  
//        Connection conn = null;  
//        try {  
//            Class.forName("com.mysql.jdbc.Driver");  
//            String url = "jdbc:mysql://localhost:3306/officialcar";  
//            String username = "root";  
//            String password = "19960630";  
//            conn = DriverManager.getConnection(url, username, password);   
//        } catch (ClassNotFoundException e) {  
//            e.printStackTrace();  
//        } catch (SQLException e) {  
//            e.printStackTrace();  
//        }  
//        return conn;  
//    }  
	public List<UserBean> find(int page){
		List<UserBean> list =new ArrayList<UserBean>();
		Connection con=DBUtil.getConnection();
		String sql="select * from userinformation order by id desc limit ?,?";
	      try{
	    	  PreparedStatement ptmt=con.prepareStatement(sql);
	    	  ptmt.setInt(1,(page-1)*UserBean.Pagesize);
	    	  ptmt.setInt(2,UserBean.Pagesize);
	    	  ResultSet rs=ptmt.executeQuery();
	    	  while(rs.next()){
	    		  UserBean p=new  UserBean();
	    		  p.setId(rs.getInt("id"));
	    		  p.setName(rs.getString("name"));
	    		  p.setPassword(rs.getString("password"));
	    		  p.setName1(rs.getString("name1"));
	    		  p.setNumber(rs.getString("number"));
	    		  p.setAddress(rs.getString("address"));
	    		  p.setIdcard(rs.getString("idcard"));
	    		  p.setJob(rs.getString("job"));
	    		  list.add(p);
	    	  }
	    	  rs.close();
	    	  ptmt.close();
	    	  con.close();
	      }catch(SQLException e){
	    	  e.printStackTrace();
	      }
	      return list;
	}
	public int findCount(){
		 int count=0;
		 Connection con=DBUtil.getConnection();
		 String sql="select count(*) from userinformation";
		  try{
			  Statement stmt=(Statement)con.createStatement();
			  ResultSet rs=stmt.executeQuery(sql);
			  if(rs.next()){
				  count=rs.getInt(1);
			  }
			  rs.close();
			  con.close();
		  }catch (SQLException e){
	    	  e.printStackTrace();
	      }
		  return count;
	}
}
